import Vue from "vue";
import Router from "vue-router";
// import { MessageBox } from "element-ui";
import store from "../store/index";

// 路由懒加载
// import Login from "../views/Login.vue";
const Login = () => import(/* webpackChunkName: "login_register_main" */ "../views/Login.vue");
// import Register from "../views/Register.vue";
const Register = () => import(/* webpackChunkName: "login_register_main" */ "../views/Register.vue");
// import Main from "../views/Main.vue";
const Main = () => import(/* webpackChunkName: "login_register_main" */ "../views/Main.vue");

// import Center from "../views/Center.vue";
const Center = () => import(/* webpackChunkName: "center_profile_order_pwdModify" */ "../views/Center.vue");
// import Profile from "../views/Profile.vue";
const Profile = () => import(/* webpackChunkName: "center_profile_order_pwdModify" */ "../views/Profile.vue");
// import Order from "../views/Order.vue";
const Order = () => import(/* webpackChunkName: "center_profile_order_pwdModify" */ "../views/Order.vue");

// import Pay from "../views/Pay.vue";
const Pay = () => import(/* webpackChunkName: "pay_record_myOrder" */ "../views/Pay.vue");
// import Record from "../views/Record.vue";
const Record = () => import(/* webpackChunkName: "pay_record_myOrder" */ "../views/Record.vue");
// import MyOrder from "../views/MyOrder.vue";
const MyOrder = () => import(/* webpackChunkName: "pay_record_myOrder" */ "../views/MyOrder.vue");

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
//获取原型对象上的push函数
const originalPush = Router.prototype.push;
//修改原型对象中的push方法
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

Vue.use(Router);

const router = new Router({
  routes: [
    { path: "/", name: "main", component: Main },
    { path: "/login", name: "login", component: Login },
    { path: "/register", name: "register", component: Register },
    {
      path: "/center",
      name: "center",
      component: Center,
      children: [
        { path: "profile", component: Profile },
        { path: "order", component: Order },
        { path: "order/:id", component: Order, props: true },
        { path: "pay", component: Pay },
        { path: "record", component: Record },
        { path: "myOrder", component: MyOrder },
      ],
    },
  ],
});

router.beforeEach((to, from, next) => {
  if (to.name !== "login" && !store.state.token) {
    next("/login");
  } else if (to.name !== "login" && store.state.code.includes("ERROR")) {
    router.app.$alert(store.state.msg, "", {
      confirmButtonText: "确定",
      callback: () => {
        store.commit("Logout");
        next("/login");
      },
    });
  } else if (to.name !== "login" && store.state.code != "0" && !store.state.code.includes("ERROR")) {
    router.app.$alert("出现错误，请重新登录", "", {
      confirmButtonText: "确定",
      callback: () => {
        store.commit("Logout");
        next("/login");
      },
    });
  } else {
    next();
  }
});

export default router;
